﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RESP_IIWaterAssessment
{
    public partial class AVPHarvestForm : Form
    {
        public AVPHarvestForm()
        {
            InitializeComponent();
        }

        int regID, avpID, cultID, year;
        tb_AVPHarvest avpHarvest;

        private void AVPHarvestForm_Load(object sender, EventArgs e)
        {
            cbRegion.SelectedIndexChanged -= cbRegion_SelectedIndexChanged;
            cbRegion.DataSource = UtilClass.FillCBRegions();
            cbRegion.DisplayMember = "Name";
            cbRegion.ValueMember = "ID";

            if (InformationClass.isAdmin)
            {
                cbRegion.SelectedIndex = -1;
                cbRegion.Text = string.Empty;
                cbRegion.SelectedIndexChanged += new EventHandler(cbRegion_SelectedIndexChanged);
            }
            else
            {
                cbRegion.Enabled = false;
                regID = InformationClass.regionID;
                cbRegion.SelectedIndexChanged += new EventHandler(cbRegion_SelectedIndexChanged);
                cbRegion.SelectedValue = regID;
                cbRegion_SelectedIndexChanged(cbRegion, null);
                cbAVP.Enabled = true;
                cbAVP.Text = string.Empty;
                //toolStrip1.Visible = false;
            }

            for (int i = 1991; i < 2100; i++)
            {
                cbYear.Items.Add(i);
            }
            cbYear.SelectedIndex = cbYear.FindStringExact(DateTime.Now.Year.ToString());

            cbCulture.SelectedIndexChanged -= cbCulture_SelectedIndexChanged;
            cbCulture.DataSource = UtilClass.GetCBCultureDataSourse();
            cbCulture.DisplayMember = "Name";
            cbCulture.ValueMember = "ID";
            cbCulture.SelectedIndex = -1;
            cbCulture.SelectedIndexChanged += new EventHandler(cbCulture_SelectedIndexChanged);
        }

        private void cbRegion_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbRegion.SelectedIndex >= 0)
            {
                cbAVP.Enabled = true;
                regID = Convert.ToInt32(cbRegion.SelectedValue);
                cbAVP.SelectedIndexChanged -= cbAVP_SelectedIndexChanged;
                cbAVP.DataSource = UtilClass.FillAVP(regID);
                cbAVP.ValueMember = "ID";
                cbAVP.DisplayMember = "Name";
                cbAVP.SelectedIndex = -1;
                cbAVP.Text = string.Empty;
                cbAVP.SelectedIndexChanged += new EventHandler(cbAVP_SelectedIndexChanged);
                tbArea.Text = tbGathering.Text = string.Empty;
            }
        }

        private void cbAVP_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbAVP.SelectedIndex >= 0)
            {
                avpID = Convert.ToInt32(cbAVP.SelectedValue);
                ShowAVPHarvest(cultID, year, avpID);
            }
            else
                avpID = 0;
        }

        private void cbCulture_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbCulture.SelectedIndex >= 0)
            {
                cultID = Convert.ToInt32(cbCulture.SelectedValue);
                ShowAVPHarvest(cultID, year, avpID);
            }
            else
                cultID = 0;
        }

        private void cbYear_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbYear.SelectedIndex >= 0)
            {
                year = Convert.ToInt32(cbYear.Items[cbYear.SelectedIndex].ToString());
                ShowAVPHarvest(cultID, year, avpID);
            }
            else
                year = DateTime.Now.Year;
        }

        private void ShowAVPHarvest(int cultID, int year, int avpID)
        {
            if (avpID > 0 && cultID > 0 && year >= 1991)
            {
                avpHarvest = DBClass.AVPHarvest
                    .Where(wh => wh.AVPID == avpID && wh.CultureID == cultID && wh.Year == year)
                    .SingleOrDefault();
                if (avpHarvest != null)
                {
                    tbArea.Text = avpHarvest.Area.ToString();
                    tbGathering.Text = avpHarvest.Gathering.ToString();
                }
                else
                {
                    tbGathering.Text = tbArea.Text = string.Empty;
                }
            }
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (avpID > 0 && cultID > 0 && year >= 1991)
            {
                decimal area = 0, gathering = 0;
                if (!(tbArea.Text.GetDecimalOrDefault(out area) && tbGathering.Text.GetDecimalOrDefault(out gathering)))
                    if (MessageBox.Show("Не верно заполненны данные о сборе. Продолжить?", "Внимание", MessageBoxButtons.YesNo) == DialogResult.No)
                        return;

                if (avpHarvest != null)
                {
                    avpHarvest.Gathering = gathering;
                    avpHarvest.Area = area;
                }
                else
                {
                    avpHarvest = new tb_AVPHarvest
                    {
                        Year = year,
                        Gathering = gathering,
                        CultureID = cultID,
                        AVPID = avpID,
                        Area = area
                    };
                    DBClass.AVPHarvest.InsertOnSubmit(avpHarvest);
                }
                try
                {
                    DBClass.RESPContext.SubmitChanges();
                    MessageBox.Show(msgClass.msgSubmitChanges, msgClass.msgAchtung);
                }
                catch
                {
                    MessageBox.Show(msgClass.msgError, msgClass.msgAchtung);
                }
            }
        }
    }
}
